/*
 *  Copyright (c)  2016-NOW  Jacars Co.Ltd.
 *  All rights reserved.
 */
package com.jacars.biz.user.dao.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jacars.biz.user.dao.model.JsptDept;
import com.jacars.biz.user.dao.service.JsptDeptService;
import com.jacars.biz.user.dao.mapper.JsptDeptMapper;
import com.jacars.common.utils.FuncUtil;
import com.jacars.common.utils.node.ForestNodeMerger;
import com.jacars.biz.user.vo.JsptDeptVO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务实现类
 *
 * @author Chill
 */
@Service
public class JsptDeptServiceImpl extends ServiceImpl<JsptDeptMapper, JsptDept> implements JsptDeptService {

	@Override
	public IPage<JsptDeptVO> selectDeptPage(IPage<JsptDeptVO> page, JsptDeptVO dept) {
		return page.setRecords(baseMapper.selectDeptPage(page, dept));
	}

	@Override
	public List<JsptDeptVO> tree(String tenantId) {
		return ForestNodeMerger.merge(baseMapper.tree(tenantId));
	}

	@Override
	public String getDeptIds(String tenantId, String deptNames) {
		List<JsptDept> jsptDeptList = baseMapper.selectList(Wrappers.<JsptDept>query().lambda().eq(JsptDept::getTenantId, tenantId).in(JsptDept::getDeptName, FuncUtil.toStrList(deptNames)));
		if (jsptDeptList != null && jsptDeptList.size() > 0) {
			return jsptDeptList.stream().map(dept -> FuncUtil.toStr(dept.getId())).distinct().collect(Collectors.joining(","));
		}
		return null;
	}

	@Override
	public List<String> getDeptNames(String deptIds) {
		return baseMapper.getDeptNames(FuncUtil.toLongArray(deptIds));
	}

}
